<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .addInput div {
            margin-top: 30px;
        }
        

        .title {
            -webkit-appearance: none;
            background-color: #fff;
            background-image: none;
            border-radius: 4px;
            border: 1px solid #dcdfe6;
            box-sizing: border-box;
            color: #606266;
            display: inline-block;
            font-size: inherit;
            height: 40px;
            line-height: 40px;
            outline: none;
            padding: 0 15px;
        }

        .content {
            display: block;
            resize: vertical;
            padding: 5px 15px;
            line-height: 1.5;
            box-sizing: border-box;
            width: 300px;
            height: 100px;
            font-size: inherit;
            color: #606266;
            background-color: #fff;
            background-image: none;
            border: 1px solid #dcdfe6;
            border-radius: 4px;
        }

        .addInput div input:focus {
            outline: none;
            border-color: #409eff;
        }

        .contentContainer {
            display: flex;
        }

        .selectStyle {
            background-color: #fff;
            background-image: none;
            border-radius: 4px;
            border: 1px solid #dcdfe6;
            box-sizing: border-box;
            color: #606266;
            display: inline-block;
            font-size: inherit;
            height: 40px;
            line-height: 40px;
            outline: none;
            padding: 0 15px;
            width: 200px;
        }

        .fileinput-button {
            position: relative;
            display: inline-block;
            overflow: hidden;
            background: #409eff;
            padding: 9px 15px;
            font-size: 12px;
            border-radius: 3px;
            color: white;
        }

        .fileinput-button input {
            position: absolute;
            right: 0px;
            top: 0px;
            opacity: 0;
            font-size: 50px;
        }

        .btn {
            background-color: #409eff;
            color: white;
            display: inline-block;
            line-height: 1;
            white-space: nowrap;
            cursor: pointer;
            border: 1px solid #dcdfe6;
            -webkit-appearance: none;
            text-align: center;
            box-sizing: border-box;
            outline: none;
            margin: 0;
            transition: .1s;
            font-weight: 500;
            padding: 12px 20px;
            font-size: 14px;
            border-radius: 4px;
        }
    </style>
    <script src="../../js/ajax.js"></script>
</head>

<body>
    <div class="menu-content">
        <div class="addInput">
            <div>
				<!-- mvc module view controller -->
                商品名：<input class="title" id="pname" name="title" type="text" />
            </div>
            <div class="contentContainer">
                价格：<input class="title" id="price" name="title" type="text" />
            </div>
            <div>
                类型：<select class="selectStyle" name="type" id="type">
                   
                    </select>
            </div>
            
            <div class="uploadContainer">
                缩略图：
                <span class="fileinput-button">
                    <span>上传图片</span>
                    <input class="imgFile" type="file" name="myimg" />
                </span>
                <!-- <img src="" alt="" class="img"> -->
            </div>
            <div>
                <button class="btn" type="submit">提交</button>
            </div>
        </div>
    </div>
    <script>
        ajax({
            url:"ptypesel.php",
            dataType:"text",
            success(data){
                document.querySelector("#type").innerHTML = data;
            }
        });
        let btnEle = document.querySelector(".btn");
        btnEle.onclick = function (){
            let pname = document.querySelector("#pname").value;
            let price = parseFloat(document.querySelector("#price").value);
            let type = document.querySelector("#type").value;
            let imgFile = document.querySelector(".imgFile").files[0];
            let form = new FormData();
            form.append("pname",pname);
            form.append("price",price);
            form.append("type",type);
            form.append("img",imgFile);

            let xhr ; 
            if( window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            } else {
                xhr = new ActiveXObject();
            }
            xhr.open("post","./add.php",true);
            
            
            xhr.onload = function (){
                let data = JSON.parse(xhr.responseText);
                if ( data['status'] == 1 ){
                    window.alert("添加成功");
                    window.location.href = './admin.html';
                }
            }
            xhr.send(form);
        }
    </script>
</body>

</html>